home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10116 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: #define "creating" strings ?
  5. Date: 15 Mar 1996 17:51:43 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar15105143@qcd.lanl.gov>
  8. References: <Do9tsI.H2t@undergrad.math.uwaterloo.ca> <4ia60f$dhq@ringer.cs.utsa.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: nreitzel@lonestar.jpl.utsa.edu's message of 14 Mar 1996 22:18:55 GMT
  13.  
  14. In article <4ia60f$dhq@ringer.cs.utsa.edu>
  15. nreitzel@lonestar.jpl.utsa.edu (Norman L. Reitzel   ) writes:
  16.  
  17. NLR: In article <Do9tsI.H2t@undergrad.math.uwaterloo.ca>
  18. crpalmer@solo.uwaterloo.ca (Chris Palmer) writes: 
  19. NLR: >I have a problem that I would like to solve using a "feature" of the C
  20. NLR: >preprocessor that until now I've never seen or heard of.  A book that we have
  21. NLR: >here gives the example:
  22. NLR: >
  23. NLR: >#define MAKESTRING(x) "x"
  24. NLR: >
  25. NLR: >which does in fact produce strings that have the value of x substituted.
  26. NLR: >[eg: MAKESTRING(foo) gets processed into "foo"].
  27. NLR: >
  28. NLR: >I've verified this on a DEC Alpha (cc) and the GNU C compiler..
  29.  
  30. You probably didn't tell GNU C to compile C. For example `gcc
  31. -traditional' compiles a language which was a predecessor of C: it is
  32. now commonly called `traditional C' or `K&R I C'. `gcc' alone usually
  33. compiles GNU C. Only when you  say `gcc -ansi -pedantic' does it try
  34. to compile C.
  35.  
  36. NLR: >
  37. NLR: >Is this in fact a supported behaviour that should be portable to most
  38. NLR: >C compilers?
  39. NLR: 
  40. NLR: Sure.  Why is this surprizing?  The preprocessor performs character 
  41. NLR: string substitution, a la M4.  It doesn't care if it's assembling a 
  42. NLR: string with quotes or a syntactic disaster.
  43.  
  44. This was the behaviour of older compilers, but it is not true in the
  45. language currently called C.
  46.  
  47. The preprocessor does not perform character string substitution. (In
  48. fact I do not even know what you mean by that: after #define h j, can
  49. I write "hello" in my code instead of "jello"? Think before you
  50. explain incorrectly).
  51.  
  52. Preprocessing works on what are called preprocessing tokens. It
  53. replace one set of pp-tokens with another ... though the new set
  54. of _pp-tokens_ may indeed not form a syntactically valid C fragment. 
  55.  
  56. Preprocessing does not change `parts' of pp-tokens. An entire string
  57. is a single pp-token and hence its parts can't be changed.
  58.  
  59. #define MAKESTRING(x) #x
  60.  
  61. will, in any ANSI conformant system, have the desired result. Read the
  62. FAQ for more details.
  63.  
  64. Cheers
  65. Tanmoy
  66. --
  67. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  68. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  69. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  70. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  71. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  72. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  73.